//
//  BAHomeRetweetView.swift
//  BAweibo
//
//  Created by baggio on 2017/8/19.
//  Copyright © 2017年 baggio. All rights reserved.
//

import UIKit
import SnapKit

class BAHomeRetweetView: UIView {
    
    private var bottomConstraint: Constraint?
    
    //提供一个属性供外界赋值
    var homeViewModel: BAHomeViewModel? {
        didSet{
//            contentLabel.text = homeViewModel?.homeModel?.retweeted_status?.text
            //前面加 @被转帖的发帖人：\n
            contentLabel.text = "@\(homeViewModel?.homeModel?.retweeted_status?.user?.name ?? "" )：\n\(homeViewModel?.homeModel?.retweeted_status?.text ?? "" )"
            
            //配图
            //卸载约束
            bottomConstraint?.deactivate()
            if let pic_urls = homeViewModel?.homeModel?.retweeted_status?.pic_urls, pic_urls.count > 0 {
                //赋值
                pictureView.picUrls = pic_urls
                //更新约束
                self.snp.makeConstraints({ (m) in
                    bottomConstraint = m.bottom.equalTo(pictureView.snp.bottom).offset(BA_HOME_CELL_MARGIN).constraint
                })
                //显示
                pictureView.isHidden = false
            } else {
                //更新约束
                self.snp.makeConstraints({ (m) in
                    bottomConstraint = m.bottom.equalTo(contentLabel.snp.bottom).offset(BA_HOME_CELL_MARGIN).constraint
                })
                pictureView.isHidden = true
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        //测试
//        backgroundColor = randomColor()
        backgroundColor = RGB(red: 235, green: 235, blue: 235)
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp.makeConstraints { (m) in
            m.left.equalTo(self).offset(BA_HOME_CELL_MARGIN)
            m.top.equalTo(self).offset(BA_HOME_CELL_MARGIN)
            m.right.equalTo(self).offset(-BA_HOME_CELL_MARGIN)
        }
        
        pictureView.snp.makeConstraints { (m) in
            m.left.equalTo(contentLabel)
            m.top.equalTo(contentLabel.snp.bottom).offset(BA_HOME_CELL_MARGIN)
            m.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp.makeConstraints { (m) in
            //m.bottom.equalTo(contentLabel).offset(BA_HOME_CELL_MARGIN)
            //加了配图后
            bottomConstraint = m.bottom.equalTo(pictureView).offset(BA_HOME_CELL_MARGIN).constraint
            
        }
    }

    //转发微博的正文
    private lazy var contentLabel: UILabel = UILabel(textAlignment: nil, text: "转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文转发微博正文", textColor: UIColor.darkGray, fontSize: 14, numberOfLines: 0, alpha: nil)
    
    //转发微博的配图，原创微博的配图一样（除了数据）
//    private lazy var pictureView: BAHomePictureView = BAHomePictureView()
    //配图使用 originalView 或 originalView 的背景色（可以使用 透明色，但是透明色性能不好，一般只用于图片只渲染一次的情况）
    private lazy var pictureView: BAHomePictureView = {
        let p = BAHomePictureView()
        p.backgroundColor = self.backgroundColor //self. 不会联想
        return p
    }()
}
